package Jets.utils.display
{
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * 位图的九宫格裁剪器 
	 * @author Administrator
	 * struct Res_png_9patch  
	 {  ...  
	 int8_t wasDeserialized;  
	 int8_t numXDivs;  
	 int8_t numYDivs;  
	 int8_t numColors;  
	 // These tell where the next section of a patch starts.  
	 // For example, the first patch includes the pixels from  
	 // 0 to xDivs[0]-1 and the second patch includes the pixels  
	 // from xDivs[0] to xDivs[1]-1.  
	 // Note: allocation/free of these pointers is left to the caller.  
	 int32_t* xDivs;  
	 int32_t* yDivs;  
	 int32_t paddingLeft, paddingRight;  
	 int32_t paddingTop, paddingBottom;  
	 enum {  
	 // The 9 patch segment is not a solid color.  
	 NO_COLOR = 0x00000001,  
	 
	 // The 9 patch segment is completely transparent.  
	 TRANSPARENT_COLOR = 0x00000000  
	 };  
	 // Note: allocation/free of this pointer is left to the caller.  
	 uint32_t* colors;     
	 }  
	 * 
	 */
	public class BitmapGrid9Util
	{
		static public function grid9(bitmapData:BitmapData,width:Number,height:Number,rect:Rectangle,isSmoothing:Boolean=false):BitmapData
		{
			var leftSpace: int = rect.left;
			var topSpace: int = rect.top;
			var rightSpace: int= rect.right;
			var bottomSpace: int = rect.bottom;
			
			//检查分割参数的有效性
			checkScaleArguments(bitmapData.width, bitmapData.height, leftSpace, topSpace, rightSpace, bottomSpace);
			
			var nCenterWidth: int = bitmapData.width - leftSpace - rightSpace;
			var nCenterHeight: int = bitmapData.height - topSpace - bottomSpace;
			
			//生成9个位置的位图
			var m_TopLeftData: BitmapData = copyBitmapData(bitmapData, 0, 0, leftSpace, topSpace);
			var m_TopData: BitmapData = copyBitmapData(bitmapData, leftSpace, 0, nCenterWidth, topSpace);
			var m_TopRightData: BitmapData = copyBitmapData(bitmapData, bitmapData.width - rightSpace, 0, rightSpace, topSpace);
			var m_LeftData: BitmapData = copyBitmapData(bitmapData, 0, topSpace, leftSpace, nCenterHeight);
			var m_CenterData: BitmapData = copyBitmapData(bitmapData, leftSpace, topSpace, nCenterWidth, nCenterHeight);
			var m_RightData: BitmapData = copyBitmapData(bitmapData, bitmapData.width - rightSpace, topSpace, rightSpace, nCenterHeight);
			var m_BottomLeftData: BitmapData  = copyBitmapData(bitmapData, 0, bitmapData.height - bottomSpace, leftSpace, bottomSpace);
			var m_BottomData: BitmapData = copyBitmapData(bitmapData, leftSpace, bitmapData.height - bottomSpace, nCenterWidth, bottomSpace);
			var m_BottomRightData: BitmapData = copyBitmapData(bitmapData, bitmapData.width - rightSpace, bitmapData.height - bottomSpace, rightSpace, bottomSpace);
			
			var Result: BitmapData = new BitmapData(width, height, true, 0);
			
			var src: Rectangle = new Rectangle;
			var dest: Point = new Point;
			var mt: Matrix = new Matrix();
			var bmData: BitmapData = bitmapData;
			var bmWidth: int = bmData.width;
			var bmHeight: int = bmData.height;
			var bmHWidth: int = bmWidth - leftSpace - rightSpace;
			var bmVHeight: int = bmHeight - topSpace - bottomSpace;
			var topScale: Number = (width - leftSpace - rightSpace)/bmHWidth;
			var leftScale: Number = (height - topSpace - bottomSpace)/bmVHeight;
			
			//top;
			mt.tx = leftSpace;
			mt.a = topScale;
			Result.draw(m_TopData, mt, null, null, null, isSmoothing);
			
			//bottom
			mt.ty = height - bottomSpace;
			Result.draw(m_BottomData, mt, null, null, null, isSmoothing);
			
			//left
			mt.tx = 0;
			mt.ty = topSpace;
			mt.a = 1;
			mt.d = leftScale;
			Result.draw(m_LeftData, mt, null, null, null, isSmoothing);
			
			//right
			mt.tx = width - rightSpace;
			Result.draw(m_RightData, mt, null, null, null, isSmoothing);
			
			//center
			mt.tx = leftSpace;
			mt.ty = topSpace;
			mt.a = topScale;
			mt.d = leftScale;
			Result.draw(m_CenterData, mt, null, null, null, isSmoothing);
			
			//top-left
			src.width = leftSpace;
			src.height = topSpace;
			Result.copyPixels(bmData, src, dest);
			
			//top-right
			src.x = bmWidth - rightSpace;
			src.width = rightSpace;
			dest.x = width - rightSpace;
			Result.copyPixels(bmData, src, dest);
			
			//bottom-left
			src.x = 0;
			src.y = bmHeight - bottomSpace;
			src.width = leftSpace;
			src.height = bottomSpace;
			dest.x = 0;
			dest.y = height - bottomSpace;
			Result.copyPixels(bmData, src, dest);
			
			//bottom-right
			src.x = bmWidth - rightSpace;
			dest.x = width - rightSpace;
			src.width = rightSpace;
			Result.copyPixels(bmData, src, dest);
			
			return Result;
		}
		
		
		/**
		 * 从bitmapData拷贝一个矩形区域并生成新的位图 
		 * @param bitmapData
		 * @param x
		 * @param y
		 * @param width
		 * @param height
		 * @return 
		 * 
		 */
		static private function copyBitmapData(bitmapData: BitmapData, x: int, y: int, width: int, height: int): BitmapData
		{
			var newBitmapData: BitmapData = new BitmapData(width, height);
			newBitmapData.copyPixels(bitmapData, new Rectangle(x, y, width, height), new Point(0, 0));
			return newBitmapData;
		}
		
		/**
		 * 检查指定的九宫格分割参数是否有效 
		 * 如果参数无效则会抛出异常
		 * @param width
		 * @param height
		 * @param scale9GridLeft
		 * @param scale9GridTop
		 * @param scale9Right
		 * @param scale9Bottom
		 * 
		 */
		static private function checkScaleArguments(width: int, height: int, leftSpace: int, topSpace: int, rightSpace: int, bottomSpace: int): void
		{
			if ( leftSpace <= 0 || topSpace <= 0 || rightSpace <= 0 || bottomSpace <= 0
				|| leftSpace + rightSpace > width || topSpace + bottomSpace > height )
			{
				throw new ArgumentError("无效的九宫格分割参数"); 
			}
		}
	}
}


